package com.yze.headline.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.yze.headline.R;

public class HomeFragment extends Fragment {

    PagerSlidingTabStrip tabs;
    ViewPager viewPager;
    View view;
    HeadLineAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView(){
        tabs=(PagerSlidingTabStrip)view.findViewById(R.id.tabs);
        viewPager=(ViewPager)view.findViewById(R.id.viewPager);

        //初始化适配器
        adapter=new HeadLineAdapter(getActivity().getSupportFragmentManager());
        //将适配器绑定到ViewPager
        viewPager.setAdapter(adapter);
        //关联viewpager
        tabs.setViewPager(viewPager);

    }

    private class HeadLineAdapter extends FragmentPagerAdapter{
        //标题集合
        private final String[] TITLES = {"推荐","热点","视频","北京","社会","娱乐","问答"
                ,"图片","科技","汽车","体育","财经","军事","国际","段子","趣图","美女","健康"};

        public HeadLineAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return NewsFragment.newInstance(TITLES[position],"");
        }

        /**
         * 获取对应标题
         * @param position
         * @return
         */

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }
    }

}
